//
//  ViewController.m
//  SuperLight
//
//  Created by Charlie Liao on 14-4-24.
//  Copyright (c) 2014年 Charlie Liao. All rights reserved.
//

#import "MainViewController.h"
#import "LightsTableVC.h"
#import "FunctionVC.h"

static int colorArray[] = { 0xFFFF0000, 0xFFFF3300, 0xFFFF6600,
    0xFFFF9900, 0xFFFFCC00, 0xFFFFFF00, 0xFFCCFF00, 0xFF99FF00,
    0xFF66FF00, 0xFF33FF00, 0xFF00FF99, 0xFF00FFFF, 0xFF00CCFF,
    0xFF0099FF, 0xFF0033FF, 0xFF0000FF, 0xFF3300FF, 0xFF6600FF,
    0xFF9900FF, 0xFFFF00FF, 0xFFFF00CC, 0xFFFF0099, 0xFFFF0099,
    0xFFFF0033, 0xFFCC3333, 0xFFC4723C, 0xFFC4A93C, 0xFFC4C43C,
    0xFF8CBB44, 0xFF55AA66, 0xFF4DB38A, 0xFF5599AA, 0xFF5566AA,
    0xFF4D4DB3, 0xFF7755AA, 0xFF9955AA, 0xFFAA5577, 0xFFB34D61,
    0xFF996699, 0xFFC4723C, 0xFFC4A93C, 0xFFC4C43C };

@interface MainViewController ()
{
    NSMutableArray *selectArr;
}

@end

@implementation MainViewController

- (UIColor*)intValueToColor:(int)c
{
    Byte a = (Byte) (c >> 24 & 0xff);
    Byte r = (Byte) (c >> 16 & 0xff);
    Byte g = (Byte) (c >> 8 & 0xff);
    Byte b = (Byte) (c & 0xff);
    
    return RGBAColor(r, g, b, a);
}

#pragma mark  Init

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        selectArr = [NSMutableArray array];
        lightController = [LightController shareInstance];
    }
    return self;
}

#pragma mark Life cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavigation];
    
    [self.colorCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"testcell"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [selectArr removeAllObjects];
    
    for (LightEntity *entity in [Common share].lightsArr) {
        if (entity.isSelect) {
            [selectArr addObject:entity];
        }
    }
    
    if (selectArr.count == 0) {
        self.title = @"No lights";
    } else if (selectArr.count == 1) {
        self.title = [(LightEntity*)selectArr[0] name];
    } else {
        self.title = @"Multiple lights";
    }
}

/**
 * 设置导航栏
 */
- (void)setupNavigation
{
    self.navigationController.navigationBar.translucent = YES;
    self.navigationItem.leftBarButtonItem =
        [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"list_btn"] style:UIBarButtonItemStylePlain target:self action:@selector(onClickListBtn)];
    
    self.navigationItem.rightBarButtonItem =
    [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"more_btn"] style:UIBarButtonItemStylePlain target:self action:@selector(onClickFunctionBtn)];
}


#pragma mark   UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 36;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"testcell" forIndexPath:indexPath];
   
    cell.backgroundColor = [self intValueToColor:colorArray[indexPath.row]];
    return cell;
}

#pragma mark    UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
//    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    int c = colorArray[indexPath.row];
    Byte r = (Byte) (c >> 16 & 0xff);
    Byte g = (Byte) (c >> 8 & 0xff);
    Byte b = (Byte) (c & 0xff);
    
    for (LightEntity* entity in selectArr) {
      [lightController setLightColorR:r g:g b:b addr:entity.addr];
    }
    
//    NSLog(@"color->%@", cell.backgroundColor);
}

#pragma mark Action

- (void)onClickListBtn
{
    LightsTableVC *vc = [[LightsTableVC alloc] initWithStyle:UITableViewStylePlain];
    
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)onClickFunctionBtn
{
    FunctionVC* vc = [[FunctionVC alloc] initWithNibName:@"FunctionVC" bundle:nil];
    vc.brightness = self.brightnessSlider.value;

    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)onClickOnBtn:(id)sender {
    
    for (LightEntity* entity in selectArr) {
        [lightController turnOnLights:entity.addr];
    }
}

- (IBAction)onClickOffBtn:(id)sender {
    for (LightEntity* entity in selectArr) {
        [lightController turnOffLights:entity.addr];
    }
}

- (IBAction)onClickWhiteBtn:(id)sender
{
    for (LightEntity* entity in selectArr) {
        [lightController setLightOnlyWhite:entity.addr brightness:self.brightnessSlider.value];
    }
}

- (IBAction)onClickRedBtn:(id)sender
{
    for (LightEntity* entity in selectArr) {
        [lightController setLightOnlyRed:entity.addr];
    }
}

- (IBAction)onClickGreenBtn:(id)sender
{
    for (LightEntity* entity in selectArr) {
        [lightController setLightOnlyGreen: entity.addr];
    }
}

- (IBAction)onClickBlueBtn:(id)sender
{
    for (LightEntity* entity in selectArr) {
        [lightController setLightOnlyBlue: entity.addr];
    }
}

- (IBAction)brightnessValueChanged:(id)sender
{
    UISlider *slider = sender;
    for (LightEntity* entity in selectArr) {
        [lightController setLightBrightness:slider.value addr:entity.addr];
    }
}

@end
